import { AuthService } from '@service/auth.service';
import { BaseService } from '@service/base.service';
import { AbsFormUtil } from '@abstract/form';
import { Component, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { IBaseShedule } from '@model/base';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BaseScheduleEditComponent } from './base-schedule-edit/base-schedule-edit.component';

@Component({
	selector: 'app-base-schedule',
	templateUrl: './base-schedule.component.html',
	styleUrls: ['./base-schedule.component.less']
})
export class BaseScheduleComponent extends AbsFormUtil implements OnInit {

	nzLoading = false;
	data = [];

	
	constructor(
		private baseService: BaseService,
		private nzMessageService: NzMessageService,
		private fb: FormBuilder,
		private nzModalService: NzModalService,
		private authService: AuthService
	) {
		super();
		this.authList = this.authService.getAuth();
	}


	ngOnInit() {
		this.buildForm();
		this.onSearch();
	}
	buildForm() {
		this.validateForm = this.fb.group({
			beanName: [''],
		});
	}

	async onSearch(isPageInit?: boolean) {
		this.nzLoading = true;
		if (isPageInit) {
			this.page.page = 1;
		}
		const formValue = this.validateForm.value;
		const params = { ...formValue, ...this.page };
		const result = await this.baseService.getScheduleList(params);
		this.data = result.data.list;
		this.page.total = result.data.totalCount;
		this.nzLoading = false;
	}


	pageChange(isPageInit?: boolean) {
		if (isPageInit) {
			this.page.page = 1;
		}
		this.onSearch();
	}



	async del(item: IBaseShedule) {
		await this.baseService.deleteSchedule(item.id);
		this.nzMessageService.success('删除成功');
		this.onSearch(true);
	}
	async resume(item: IBaseShedule) {
		await this.baseService.resumeSchedule(item.id);
		this.nzMessageService.success('恢复成功');
		this.onSearch(true);
	}
	async pause(item: IBaseShedule) {
		await this.baseService.pauseSchedule(item.id);
		this.nzMessageService.success('暂停成功');
		this.onSearch(true);
	}
	async run(item: IBaseShedule) {
		await this.baseService.runSchedule(item.id);
		this.nzMessageService.success('执行成功');
		this.onSearch(true);
	}

	add(item?: IBaseShedule) {
		this.nzModalService.create({
			nzTitle: item ? '编辑定时任务' : '新增定时任务',
			nzContent: BaseScheduleEditComponent,
			nzStyle: {
				top: '50px'
			},
			nzWidth: '70%',
			nzComponentParams: {
				id: item?.id
			},
			nzFooter: [
				{
					label: '确定',
					type: 'primary',
					onClick: (componentInstance) => {
						(async () => {
							const formRes = componentInstance.onSubmit();
							if (formRes) {
								formRes.id = componentInstance.id;
								formRes.status = 0;
								const result = await this.baseService.saveSchedule(formRes);
								if (result.status !== 0) {
									this.nzMessageService.error('保存失败，请稍后');
									return;
								}
								componentInstance.id ? this.onSearch() : this.onSearch(true);
								this.nzMessageService.success('保存成功');
								componentInstance.destroyModal();
							}
						})();
					}
				},
				{
					label: '取消',
					onClick: componentInstance => {
						componentInstance.destroyModal();
					}
				}
			]
		})
	}

}
